package com.neu.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableRowSorter;

import com.neu.dao.impl.ModelDao;
import com.neu.dao.impl.PatientDao;
import com.neu.pojo.Model;
import com.neu.pojo.Patient;
import com.neu.pojo.Worker;
import com.neu.table.ModelTable;

import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.RowFilter;

public class ModelUI extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private ModelTable modelTable = new ModelTable();
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ModelUI frame = new ModelUI(new Worker());
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ModelUI(Worker w) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 712, 500);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		//下拉列表
		JComboBox<String> comboBox = new JComboBox<>();
		comboBox.setBounds(33, 23, 115, 39);
		contentPane.add(comboBox);
		comboBox.addItem("所有");
		comboBox.addItem("A");
		comboBox.addItem("B");
		comboBox.addItem("C");
		comboBox.addActionListener(new ActionListener() {//根据选择内容显示不同对象
			
			@Override
			public void actionPerformed(ActionEvent e) {
				TableRowSorter sorter = new TableRowSorter(modelTable);
				table.setRowSorter(sorter);
				String s=(String) comboBox.getSelectedItem();
				
				switch (s) {
			    	case "所有":
			    		break;
			    	case "A":
			    		List<RowFilter<Object, Object>> filter=new ArrayList<>(2);
				    	filter.add(RowFilter.regexFilter(s));
				    	sorter.setRowFilter(RowFilter.andFilter(filter));
				    	break;
			    	case "B":
				    	List<RowFilter<Object, Object>> filter1=new ArrayList<>(2);
				    	filter1.add(RowFilter.regexFilter(s));
				    	sorter.setRowFilter(RowFilter.andFilter(filter1));
				    	break;
				    case "C":
				    	List<RowFilter<Object, Object>> filter2=new ArrayList<>(2);
				    	filter2.add(RowFilter.regexFilter(s));
				    	sorter.setRowFilter(RowFilter.andFilter(filter2));
				    	break;
				    default:
				    	break;
				}
			}
		});
		
		JButton btnNewButton = new JButton("预览");
		btnNewButton.setBounds(33, 103, 97, 23);
		contentPane.add(btnNewButton);
		btnNewButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				//先查看几个被选中了
				int sum = 0,tar=0;
				for(int i=0;i<modelTable.getRowCount();i++) {
					if((boolean)modelTable.getValueAt(i, 0)) {
						tar = (int) modelTable.getValueAt(i, 1);//得到id
						sum++;
					}
				}
				if(sum!=1) {
					JOptionPane.showMessageDialog(null, "请单选");//提示
				}else {
					//预览
					Model model = ModelDao.getInstance().search(new Model(tar));
					CheckModel checkModel = new CheckModel(w,model);//跳转新窗口
					checkModel.setVisible(true);
					setVisible(false);
				}
			}
		});
		
		JButton btnNewButton_1 = new JButton("删除");
		btnNewButton_1.setBounds(150, 103, 97, 23);
		contentPane.add(btnNewButton_1);
		btnNewButton_1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {//先删关系，再删模板，再改表格
				
				if(modelTable.delete()>0) {
					modelTable.fireTableDataChanged();
					JOptionPane.showMessageDialog(null, "删除成功");//提示
				}else {
					JOptionPane.showMessageDialog(null, "请选中");//提示
				}
				
			}
		});
		
		JButton btnNewButton_2 = new JButton("新建模板");
		btnNewButton_2.setBounds(524, 103, 97, 23);
		contentPane.add(btnNewButton_2);
		btnNewButton_2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				AddModelUI addModelUI = new AddModelUI(w);//跳转新窗口
				addModelUI.setVisible(true);
				setVisible(false);
				
			}
		});
		
		table = new JTable(modelTable);
		table.setBounds(33, 152, 632, 215);
		contentPane.add(table);
		
		JButton btnNewButton_3 = new JButton("保存");
		btnNewButton_3.setBounds(150, 408, 97, 23);
		contentPane.add(btnNewButton_3);
		btnNewButton_3.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				for(int i=0;i<modelTable.getRowCount();i++) {
					Model m = new Model();
					m.setKind((String)modelTable.getValueAt(i,3));
					m.setName((String)modelTable.getValueAt(i, 2));
					m.setId((int)modelTable.getValueAt(i, 1));
					System.out.println(m);
					System.out.println(ModelDao.getInstance().cover(m));
				}
				JOptionPane.showMessageDialog(null, "保存成功");//提示
			}
		});
		
		JButton btnNewButton_3_1 = new JButton("返回");
		btnNewButton_3_1.setBounds(420, 408, 97, 23);
		contentPane.add(btnNewButton_3_1);
		btnNewButton_3_1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				WorkerUI workerUI = new WorkerUI(w);//跳转新窗口
				workerUI.setVisible(true);
			}
		});
		
		//窗口居中
		setLocationRelativeTo(null);
		
		//加滚动条
		JScrollPane jp = new JScrollPane(table);
		jp.setBounds(33, 152, 632, 215);
		contentPane.add(jp);
		
	}
}
